<?php
    //session_start();
   // ob_start();
    header("content-type: text/html; charset=utf-8");
	include_once("../include/function.php");
	include_once("config_admin.php");

	if(trim($_POST["chk_login"]) == "logout"){
		$sql="select * from bizit_user";
		$result=mysql_db_query($dbm, $sql);
		$col_num=mysql_num_fields($result);
		for($i=0;$i<$col_num;$i++){
			$var_cookie=NULL;
			setCookie('login_'.strtolower(mysql_field_name($result,$i)));
		}
		echo "คุณกำลังออกจากระบบ !!!";
		echo "<meta http-equiv=\"refresh\" content=\"2; url=index.php\">";
		exit;
	}
   // if($_COOKIE["login_username"] == NULL && $_COOKIE["login_password"] == NULL){
		if($_COOKIE["login_username"] ==""  && $_COOKIE["login_password"] == ""){
			
			$sql="select * from bizit_user where username='".trim($_POST["user"])."' AND password='".md5(trim($_POST["passwd"]))."'";
			$result=mysql_db_query($dbm, $sql);

			if(mysql_num_rows($result) > 0){
				$col_num=mysql_num_fields($result);
				$row = mysql_fetch_row($result);
				for($i=0;$i<$col_num;$i++){
					$var_cookie=$row[$i];
					if($row[$i] == NULL) $var_cookie="NULL";
					setCookie('login_'.strtolower(mysql_field_name($result,$i)), $var_cookie, time() + 3600 * 24 * 365);
				}
				mysql_free_result($result);
				echo "คุณกำลังเข้าสู่ระบบจัดการของ Admin";
				//print $_COOKIE["login_username"];
				echo "<meta http-equiv=\"refresh\" content=\"2; url=index.php\">";
				exit;
			}else{
				echo "คุณกรอก Username and Password ผิดพลาด";
				echo "<meta http-equiv=\"refresh\" content=\"2; url=index.php\">";
				exit;
			}
		}else{
				echo "คุณกรอก Username and Password ผิดพลาด";
				echo "<meta http-equiv=\"refresh\" content=\"2; url=index.php\">";
				exit;
		}
?>